﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using WindowsFormsApplication2.CLASS;

namespace WindowsFormsApplication2
{
    public partial class frmLogin : Form
    {
        static string read = "", read1 = "", read2 = "";
        static string name, fname,id;
       internal SqlConnection connect;
        public frmLogin(SqlConnection co)
        {
            InitializeComponent();
            connect = co;

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //this.DialogResult = DialogResult.OK;
            
                if (txtLoginName.Text != "")
                {
                    if (txtLoginName.Text == "hoogii")
                    {
                        SqlCommand comm = new SqlCommand("Select * from  Worker where WorkerLasName = '" + txtLoginName.Text + "'", connect);
                        SqlDataReader reader = comm.ExecuteReader();
                        if (reader.HasRows == true)
                        {
                            reader.Read();

                            if (reader["WorkerPass"].ToString() == txtLoginPass.Text.ToString())
                            {

                                name = reader["WorkerLasName"].ToString();
                                fname = reader["WorkerFirstName"].ToString();
                                id = reader["WorkerId"].ToString();
                                label4.Text = fname;
                                label5.Text = id;
                                comboBox1.Text = "Админ";
                                read = "asd";
                                this.DialogResult = DialogResult.OK;
                            }

                            else
                            {
                                if (txtLoginPass.Text != "")
                                {
                                    MessageBox.Show("Нууц үг буруу байна дахин оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                                else
                                {
                                    MessageBox.Show("Нууц үгээ оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                            }
                        }
                        reader.Close();
                    }
                    
                    if (read == "")
                    {
                        SqlCommand comm1 = new SqlCommand("Select * from  Worker where WorkerLasName = '" + txtLoginName.Text + "'", connect);
                        SqlDataReader reader1 = comm1.ExecuteReader();
                        if (reader1.HasRows == true)
                        {
                            reader1.Read();

                            if (reader1["WorkerPass"].ToString() == txtLoginPass.Text.ToString())
                            {

                                name = reader1["WorkerLasName"].ToString();
                                fname = reader1["WorkerFirstName"].ToString();
                                id = reader1["WorkerId"].ToString();
                                label4.Text = fname;
                                label5.Text = id;
                                comboBox1.Text = "Борлуулалтын Менежер";
                                read1 = "";
                                this.DialogResult = DialogResult.OK;

                            }

                            else
                            {
                                if (txtLoginPass.Text != "")
                                {
                                    MessageBox.Show("Нууц үг буруу байна дахин оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                                else
                                {
                                    MessageBox.Show("Нууц үгээ оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                            }
                        } reader1.Close();

                        
                    } 
                    if (read =="" & read1 == "")
                    {
                        SqlCommand comm2 = new SqlCommand("Select * from  XTolologch where XTolologchUName = '" + txtLoginName.Text + "'", connect);
                        SqlDataReader reader2 = comm2.ExecuteReader();
                        if (reader2.HasRows == true)
                        {
                            reader2.Read();

                            if (reader2["XTolologchPass"].ToString() == txtLoginPass.Text.ToString())
                            {

                                name = reader2["XTolologchUName"].ToString();
                                fname = reader2["XTolologchName"].ToString();
                                id = reader2["XTolologchId"].ToString();
                                label4.Text = fname;
                                label5.Text = id;
                                comboBox1.Text = "Худалдааны төлөөлөгч";
                                read2 = "";
                                this.DialogResult = DialogResult.OK;
                            }

                            else
                            {
                                if (txtLoginPass.Text != "")
                                {
                                    MessageBox.Show("Нууц үг буруу байна дахин оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                                else
                                {
                                    MessageBox.Show("Нууц үгээ оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                            }
                        } reader2.Close();
                    }
                    if (read == "" & read1 == "" & read2 =="")
                    {

                        SqlCommand comm3 = new SqlCommand("Select * from  Customers where CustomerLastName = '" + txtLoginName.Text + "'", connect);
                        SqlDataReader reader3 = comm3.ExecuteReader();
                        if (reader3.HasRows == true)
                        {
                            reader3.Read();

                            if (reader3["CustomerPass"].ToString() == txtLoginPass.Text.ToString())
                            {

                                name = reader3["CustomerLastName"].ToString();
                                fname = reader3["CustomerFirstName"].ToString();
                                id = reader3["CustomerId"].ToString();
                                label4.Text = fname;
                                label5.Text = id;
                                comboBox1.Text = "Харилцагч";
                                this.DialogResult = DialogResult.OK;
                            }

                            else
                            {
                                if (txtLoginPass.Text != "")
                                {
                                    MessageBox.Show("Нууц үг буруу байна дахин оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                                else
                                {
                                    MessageBox.Show("Нууц үгээ оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                            }
                        }


                        reader3.Close(); 
                    }
                                 

            }}

        private void btnLoginExit_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
            
        }

       

        private void frmLogin_Load(object sender, EventArgs e)
        {
            DBTools.ConnectDB("JOY-PC\\SQL", "tosol", "", "", DBTools.Authentication.Windows);
            DBTools._Conn.Open();
            
        }

        private void frmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Space)
            {
                this.DialogResult = DialogResult.OK;
                if (comboBox1.Text == "Админ")
                {
                    if (txtLoginName.Text != "")
                    {

                        SqlCommand comm = new SqlCommand("Select * from  Worker where WorkerLasName = '" + txtLoginName.Text + "'", connect);
                        SqlDataReader reader = comm.ExecuteReader();
                        if (reader.HasRows == true)
                        {
                            reader.Read();

                            if (reader["WorkerPass"].ToString() == txtLoginPass.Text.ToString())
                            {

                                name = reader["WorkerLasName"].ToString();
                                fname = reader["WorkerFirstName"].ToString();
                                label4.Text = fname;
                                this.DialogResult = DialogResult.OK;
                            }

                            else
                            {
                                if (txtLoginPass.Text != "")
                                {
                                    MessageBox.Show("Нууц үг буруу байна дахин оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                                else
                                {
                                    MessageBox.Show("Нууц үгээ оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                            }
                        }

                        else
                        {
                            MessageBox.Show("Хэрэглэгчийн нэр буруу байна !!!");
                            txtLoginName.Text = "";
                        }
                        reader.Close();

                    }
                }
                if (comboBox1.Text == "Борлуулалт Менежер")
                {
                    if (txtLoginName.Text != "")
                    {

                        SqlCommand comm = new SqlCommand("Select * from  Worker where WorkerLasName = '" + txtLoginName.Text + "'", connect);
                        SqlDataReader reader = comm.ExecuteReader();
                        if (reader.HasRows == true)
                        {
                            reader.Read();

                            if (reader["WorkerPass"].ToString() == txtLoginPass.Text.ToString())
                            {

                                name = reader["WorkerLasName"].ToString();
                                fname = reader["WorkerFirstName"].ToString();
                                label4.Text = fname;
                                this.DialogResult = DialogResult.OK;
                            }

                            else
                            {
                                if (txtLoginPass.Text != "")
                                {
                                    MessageBox.Show("Нууц үг буруу байна дахин оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                                else
                                {
                                    MessageBox.Show("Нууц үгээ оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                            }
                        }

                        else
                        {
                            MessageBox.Show("Хэрэглэгчийн нэр буруу байна !!!");
                            txtLoginName.Text = "";
                        }
                        reader.Close();

                    }
                }
                if (comboBox1.Text == "Худалдааны төлөөлөгч")
                {
                    if (txtLoginName.Text != "")
                    {

                        SqlCommand comm = new SqlCommand("Select * from  XTolologch where XTolologchUName = '" + txtLoginName.Text + "'", connect);
                        SqlDataReader reader = comm.ExecuteReader();
                        if (reader.HasRows == true)
                        {
                            reader.Read();

                            if (reader["XTolologchPass"].ToString() == txtLoginPass.Text.ToString())
                            {

                                name = reader["XTolologchUName"].ToString();
                                fname = reader["XTolologchName"].ToString();
                                label4.Text = fname;
                                this.DialogResult = DialogResult.OK;
                            }

                            else
                            {
                                if (txtLoginPass.Text != "")
                                {
                                    MessageBox.Show("Нууц үг буруу байна дахин оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                                else
                                {
                                    MessageBox.Show("Нууц үгээ оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                            }
                        }

                        else
                        {
                            MessageBox.Show("Хэрэглэгчийн нэр буруу байна !!!");
                            txtLoginName.Text = "";
                        }
                        reader.Close();

                    }

                }
                if (comboBox1.Text == "Харилцагч")
                {
                    if (txtLoginName.Text != "")
                    {

                        SqlCommand comm = new SqlCommand("Select * from  Customers where CustomerLastName = '" + txtLoginName.Text + "'", connect);
                        SqlDataReader reader = comm.ExecuteReader();
                        if (reader.HasRows == true)
                        {
                            reader.Read();

                            if (reader["CustomerPass"].ToString() == txtLoginPass.Text.ToString())
                            {

                                name = reader["CustomerLastName"].ToString();
                                fname = reader["CustomerFirstName"].ToString();
                                label4.Text = fname;
                                this.DialogResult = DialogResult.OK;
                            }

                            else
                            {
                                if (txtLoginPass.Text != "")
                                {
                                    MessageBox.Show("Нууц үг буруу байна дахин оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                                else
                                {
                                    MessageBox.Show("Нууц үгээ оруулна уу !!!");
                                    txtLoginPass.Text = "";
                                }
                            }
                        }

                        else
                        {
                            MessageBox.Show("Хэрэглэгчийн нэр буруу байна !!!");
                            txtLoginName.Text = "";
                        }
                        reader.Close();

                    }

                }
                if (comboBox1.Text == "") { MessageBox.Show("Хэрэглэгчийн түвшингээ сонгоно уу ! ! !"); }
            }
        }
    }
}
